<template>
	<view class="shop_home">
		<view class="home_header">
			<view class="home_photo" @click="jumpLogin">
				<image :src="uobj.img" mode="aspectFill"></image>
			</view>
			<text class="home_name">{{uobj.uname||"未登录"}}</text>
			<view class="home_msg">
				<view class="msg_tab">
					二次元认证
				</view>
				<view class="msg_tab" @click="jumpMsg">
					编辑资料
				</view>
			</view>
		</view>
		<view class="home_main">
			<view class="main_glass">
				<view class="glass_tab">
					<view>{{colNum}}</view>
					<view>收藏商品</view>
				</view>
				<view class="glass_tab">
					<view>{{allNum}}</view>
					<view>订单总数</view>
				</view>
				<view class="glass_tab">
					<view>{{paiNum}}</view>
					<view>待付款</view>
				</view>
			</view>
			<view class="main_con1">
				<view class="con1_tab" @click="jumpCol">
					<view class="tab_img">
						<image src="../../static/img/sc.png" mode=""></image>
					</view>
					<view class="tab_text">
						已收藏
					</view>
				</view>
				<view class="con1_tab" @click="jumpPaid">
					<view class="tab_img">
						<image src="/static/img/dzf.png" mode=""></image>
					</view>
					<view class="tab_text">
						待付款
					</view>
				</view>
				<view class="con1_tab" @click="jumpShipp">
					<view class="tab_img">
						<image src="/static/img/yzf.png" mode=""></image>
					</view>
					<view class="tab_text">
						待收货
					</view>
				</view>
				<view class="con1_tab" @click="jumpOrder">
					<view class="tab_img">
						<image src="/static/img/dd.png" mode=""></image>
					</view>
					<view class="tab_text">
						订单
					</view>
				</view>

			</view>
			<view class="main_con2">
				<view class="con2_ll" @click="jumpWeb">
					<text>浏览记录</text>
					<view><u-icon name="arrow-right"></u-icon></view>
				</view>
				<view class="con2_tc" @click="down">
					<text>退出登录</text>
					<view><u-icon name="arrow-right"></u-icon></view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loginFlat: false,
				uname: "未登录",
				colNum: 0,
				paiNum: 0,
				allNum: 0,
				uobj: {}
			};
		},
		async onShow() {
			if (uni.getStorageSync("_id")) {
				uni.showLoading({
					title: "加载中..."
				})
				this.uname = uni.getStorageSync("uname")
				await this.selColNum()
				await this.selPaiNum()
				await this.selAllNum()
				await this.userObj()
				uni.hideLoading()
			}
		},
		methods: {
			jumpWeb() {
				if (uni.getStorageSync("_id")) {
					uni.navigateTo({
						url: "/pages/user_web/user_web"
					})
				} else {
					uni.showToast({
						title: "请先登录",
						icon: "none"
					})
				}

			},
			jumpLogin() {

				if (!uni.getStorageSync("_id")) {
					uni.navigateTo({
						url: "/pages/login/login"
					})
				} else {
					let that = this
					uni.showLoading({
						title: "加载中..."
					})
					uni.chooseImage({
						count: 1, //默认9
						sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
						sourceType: ['album'], //从相册选择
						success: function(res) {

							let filePath = res.tempFilePaths[0];
							const result = uniCloud.uploadFile({
								filePath: filePath,
								cloudPath: String(Math.random() * 5).split('.')[1] + '.png',
							})
							result.then(res => {
								//获取到上传到云储存的url地址 
								console.log(res['fileID'])
								that.uobj.img = res['fileID']
								uniCloud.callFunction({ //客户端调用云函数，云函数调用数据库
									name: 'user_update_img', //在云函数中的函数名,也就是创建的云函数的文件夹的名字
									data: {
										_id: uni.getStorageSync(
											"_id"), //需要给那条数据使用,_id是主键,不会重复的
										fileID: res['fileID'], //图片的url地址
									},
									success: (res) => {
										console.log(res.result)
										uni.hideLoading()
									},
									error: function(e) {},
								})
							})
						}
					})

				}
			},
			jumpCol() {
				if (uni.getStorageSync("_id")) {
					uni.navigateTo({
						url: "/pages/collect/collect"
					})
				} else {
					uni.showToast({
						title: "请先登录",
						icon: "none"
					})
				}
			},
			jumpMsg() {
				if (uni.getStorageSync("_id")) {
					uni.navigateTo({
						url: "/pages/userMsg/userMsg"
					})
				} else {
					uni.showToast({
						title: "请先登录",
						icon: "none"
					})
				}
			},
			jumpPaid() {
				if (uni.getStorageSync("_id")) {
					uni.navigateTo({
						url: "/pages/paid_all/paid_all"
					})
				} else {
					uni.showToast({
						title: "请先登录",
						icon: "none"
					})
				}
			},
			jumpShipp() {
				if (uni.getStorageSync("_id")) {
					uni.navigateTo({
						url: "/pages/be_shipped/be_shipped"
					})
				} else {
					uni.showToast({
						title: "请先登录",
						icon: "none"
					})
				}
			},
			jumpOrder() {
				if (uni.getStorageSync("_id")) {
					uni.navigateTo({
						url: "/pages/order_all/order_all"
					})
				} else {
					uni.showToast({
						title: "请先登录",
						icon: "none"
					})
				}
			},
			async selColNum() {
				uniCloud.callFunction({
					name: "user_all_collect",
					data: {
						uid: uni.getStorageSync("_id")
					}
				}).then(res => {
					console.log(res.result)
					this.colNum = res.result.length
				})
			},
			async selPaiNum() {
				uniCloud.callFunction({
					name: "user_shop_bag",
					data: {
						item_id: uni.getStorageSync("_id"),
						type: 0
					}
				}).then((res) => {
					// this.paidList = res.result
					console.log(res.result.length)
					this.paiNum = res.result.length
				})
			},
			async selAllNum() {
				uniCloud.callFunction({
					name: "user_shop_bag",
					data: {
						item_id: uni.getStorageSync("_id"),
						type: 1
					}
				}).then((res) => {
					// this.paidList = res.result
					console.log(res.result.length)
					this.allNum = res.result.length
				})
			},
			async userObj() {
				uniCloud.callFunction({
					name: "sel_user",
					data: {
						_id: uni.getStorageSync("_id")
					}
				}).then((res) => {
					console.log(res.result.data[0])
					this.uobj = res.result.data[0]
				})
			},
			down() {
				console.log(123)
				uni.showModal({
					title: '请先登录',
					content: '确定注销？',
					cancelText: "取消", // 取消按钮的文字
					confirmText: "确认", // 确认按钮的文字
					showCancel: true, // 是否显示取消按钮，默认为 true
					confirmColor: '#f55850',
					cancelColor: '#333',
					success: (res) => {
						uni.showToast({
							title: "注销成功"
						})
						uni.removeStorageSync("_id");
						this.colNum = 0;
						this.paiNum = 0;
						this.allNum = 0;
						this.uobj = {}
					},

				})
			}
		}
	}
</script>

<style lang="scss">
	.shop_home {
		color: #333;
		height: 93vh;
		width: 100%;
		background-color: #f2f3f3;

		.home_name {
			font-size: 18px;
			color: white;

		}

		.home_header {
			height: 260px;
			width: 100%;
			background: url('https://mp-4a309ad7-91e7-4d22-97ea-e9113ee79467.cdn.bspapp.com/cloudstorage/343cc15a-24a8-48e8-afa0-4746e3ceac69.png') bottom center;

			background-size: cover;
			display: flex;
			align-items: center;
			flex-direction: column;
		}

		.home_photo {
			margin-top: 46px;
			height: 76px;
			width: 76px;
			background-color: #f2f3f3;
			border-radius: 50%;
			margin-bottom: 4px;
			overflow: hidden;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.home_msg {
			height: 30px;
			margin-top: 6px;
			display: flex;
			align-items: center;
			width: 170px;
			justify-content: space-between;
			font-size: 12px;

			.msg_tab {
				width: 80px;
				height: 22px;
				text-align: center;
				line-height: 22px;
				color: white;
				border-radius: 12px;
				font-weight: 100;
				background-color: rgba(255, 255, 255, .2);
			}
		}
	}

	.home_main {
		height: 200px;

		.main_glass {
			transform: translateY(-70px);
			height: 97px;
			margin: 0 10px;
			border-radius: 20px;
			position: relative;
			display: flex;

			&::before {
				position: absolute;
				top: 0;
				z-index: -1;
				left: 0;
				height: 97px;
				width: 100%;
				content: "";
				border-radius: 20px;
				backdrop-filter: blur(30px);
				// background-color: rgba(255, 255, 255, .2);
				// filter: blur(2px);
			}

			.glass_tab {
				width: 33%;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				color: white;

				&:first-child {
					margin-left: 10px;
				}

				&:last-child {
					margin-right: 10px;
				}

				view:first-child {
					font-size: 22px;
				}

				view:last-child {
					font-size: 14px;
				}
			}


		}

		.main_con1 {
			width: 350px;
			height: 80px;
			background-color: white;
			transform: translateY(-56px);
			margin: 0 auto;
			border-radius: 16px;
			display: flex;
			align-items: center;
			justify-content: space-around;

			.con1_tab {
				height: 80px;
				width: 50px;
				display: flex;
				flex-direction: column;

				align-items: center;
				justify-content: space-around;
				padding-bottom: 5px;

				.tab_img {
					margin-top: 4px;
					height: 45px;
					width: 45px;
					display: flex;
					align-items: flex-end;
					justify-content: center;

					image {
						height: 30px;
						width: 32px;


					}


				}

				&:nth-child(2) image,
				&:nth-child(3) image {
					height: 30px;
					width: 30px;
				}

				&:last-child image {
					height: 36px;
					width: 38px;
					transform: translateY(2px);
				}

				.tab_text {
					margin-bottom: 4px;
					width: 45px;
					height: 18px;
					// background-color: #f2f3f3;
					font-size: 12px;
					text-align: center;
				}
			}
		}

		.main_con2 {
			width: 350px;
			margin: 0 auto;
			transform: translateY(-35px);
			background-color: white;
			border-radius: 16px;
			display: flex;
			align-items: center;
			flex-direction: column;
			justify-content: space-between;

			.con2_ll,
			.con2_tc {
				width: 90%;
				height: 44px;
				display: flex;
				align-items: center;
				justify-content: space-between;
				font-size: 15px;
			}

			.con2_ll {
				border-bottom: 1px solid whitesmoke;
			}
		}
	}
</style>